package com.company.cloud.article.modules.mgr.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.cloud.article.modules.mgr.controller.req.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.article.modules.cache.models.ArticleModel;
import com.company.cloud.article.modules.base.entity.ArticleCompanyRelInfo;
import com.company.cloud.article.modules.base.service.ArticleCompanyRelInfoService;
import com.company.cloud.article.modules.base.service.ArticleInfoService;
import com.company.cloud.article.modules.cache.service.ArticleCacheService;
import com.company.cloud.article.modules.mgr.service.ArticleMgrService;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.utils.AuthContextUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 平台文章
 */
@Authc
@RestController
@RequestMapping("/mgr/cms/platformArticle")
@Api(tags = "Mgr_cms_平台文章管理")
public class PlatformArticleController {
	@Autowired
	private ArticleInfoService articleInfoService;
	@Autowired
	private ArticleMgrService articleMgrService;
	@Autowired
	private ArticleCacheService articleCacheService;
	@Autowired
	private ArticleCompanyRelInfoService articleCompanyRelInfoService;

	@PostMapping("/query")
	@ApiOperation(value = "查询", response = ArticleModel.class)
	public BaseResult query(@RequestBody PlatformArticleQuery4PageParam params) {
		IPage<ArticleModel> data= articleMgrService.queryPlatformArticle(params, AuthContextUtils.getUser());
		return BaseResult.ok(data);
	}

	@GetMapping("/get")
	@ApiOperation(value = "获取", response = ArticleModel.class)
	public BaseResult get(@RequestParam("articleNo") String articleNo) {
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		ArticleModel data = articleCacheService.getArticleByNoDB(articleNo,userInfo.getCompanyNo());
		return BaseResult.ok(data);
	}

	@PostMapping("/save")
	@ApiOperation(value = "新增", response = BaseResult.class)
	public BaseResult save(@RequestBody ArticleMgrSaveParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		articleMgrService.save(params, userInfo);
		return BaseResult.ok();
	}

	@PostMapping("/edit")
	@ApiOperation(value = "修改", response = BaseResult.class)
	public BaseResult edit(@RequestBody ArticleMgrUpdateParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		articleMgrService.update(params, userInfo);
		// 删除缓存
		List<ArticleCompanyRelInfo> relList = articleCompanyRelInfoService.list(
				new QueryWrapper<ArticleCompanyRelInfo>().eq("article_no", params.getArticleNo())
		);
		relList.forEach(rel->{
			articleCacheService.removeCompanyArticleByCache(rel.getArticleRelNo());
		});

		return BaseResult.ok();
	}

	@GetMapping("/delete")
	@ApiOperation(value = "删除")
	public BaseResult delete(@RequestParam("articleNo") String articleNo) {
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		articleMgrService.remove(articleNo, userInfo);
		// 删除缓存
		List<ArticleCompanyRelInfo> relList = articleCompanyRelInfoService.list(
				new QueryWrapper<ArticleCompanyRelInfo>().eq("article_no", articleNo)
		);
		relList.forEach(rel->{
			articleCacheService.removeCompanyArticleByCache(rel.getArticleRelNo());
		});
		return BaseResult.ok();
	}


	@PostMapping("/updateState")
	@ApiOperation(value = "上下架",response = BaseResult.class)
	public BaseResult updateState(@RequestBody ArticleMgrUpdateStateParam params){
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		articleMgrService.updateState(params,userInfo);
		return BaseResult.ok();
	}

	@PostMapping("/updateStateBatch")
	@ApiOperation(value = "批量上下架",response = BaseResult.class)
	public BaseResult updateStateBatch(@RequestBody ArticleMgrUpdateStateBatchParam params){
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		articleMgrService.updateStateBatch(params,userInfo);
		return BaseResult.ok();
	}

}
